﻿using AutoMapper;
using FluentValidation;
using Mes.Application.DTO.Inputs.Operations;
using Mes.Application.Services.Operations;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Operations
{
    [ApiController]
    [Route("/api/v1")]
    public class OperController : ControllerBase
    {
        private readonly ILogger<OperController> _logger;
        private readonly IOperService _operService;
        private readonly IMapper _mapper;
        private readonly IValidator<OperInput> _operInputValidator;
        private readonly IValidator<OperUpdateInput> _operUpdateInputValidator;

        public OperController(ILogger<OperController> logger,
            IOperService operService,
            IMapper mapper,
            IValidator<OperInput> operInputValidator,
            IValidator<OperUpdateInput> operUpdateInputValidator
            )
        {
            _logger = logger;
            _operService = operService;
            _mapper = mapper;
            _operInputValidator = operInputValidator;
            _operUpdateInputValidator = operUpdateInputValidator;
        }

        /// <summary>
        /// 添加工序
        /// </summary>
        /// <param name="operInput"></param>
        /// <returns></returns>
        [HttpPost("oper")]
        public async Task<ActionResult> AddOper(OperInput operInput)
        {
            try
            {
                var input = await _operInputValidator.ValidateAsync(operInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _operService.AddOperationAsync(operInput);
                return Ok();
            }catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑工序
        /// </summary>
        /// <param name="operUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("oper")]
        public async Task<ActionResult> UpdateOper(OperUpdateInput operUpdateInput)
        {
            try
            {
                var input = await _operUpdateInputValidator.ValidateAsync(operUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _operService.UpdateOperationAsync(operUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除工序
        /// </summary>
        /// <param name="process_id"></param>
        /// <param name="oper_ids"></param>
        /// <returns></returns>
        [HttpDelete("oper")]
        public async Task<ActionResult> DeleteOper(string process_id, string[] oper_ids)
        {
            try
            {
                await _operService.DeleteOperationAsync(process_id, oper_ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询指定工序
        /// </summary>
        /// <param name="process_id"></param>
        /// <param name="oper_id"></param>
        /// <returns>OperResult</returns>
        [HttpGet("oper/entity")]
        public async Task<ActionResult> GetOperEntityByOperId(string process_id, string oper_id)
        {
            try
            {
                var result = await _operService.GetOperationAsync(process_id, oper_id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
